package awesome.Manager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import awesome.security.Digest;
import org.apache.catalina.Session;

import com.sun.crypto.provider.RC2Cipher;

public class UserManager extends Manager {

	public UserManager() {
		super("user");
	}

	public void insertRow(String username,
			String first_name, String MI, String last_name, String password, String email, String address,
			String country, String city, String state, String zipcode, String phone) {

		String query = "insert into "
				+ this.tableName
				
				+ " values('','" + username + "', '" + first_name + "','" + MI
				+ "','" + last_name + "','" +password + "',"+ "'1'" +",'"+ email +"','" + address + "','"+country
				+"','" + city + "','"
				+ state + "','" + zipcode + "','" + phone + "',"
				+ "CURRENT_TIMESTAMP, '0');";
				//insert into user values('','ss', 'ss','ss','ss','ss','1','ss@s.com','ss','US','ss','MN','55987',CURRENT_TIMESTAMP)
		this.db.execute(query);	
	
	}
	
	public boolean login(String userName, String password)
	{
		String query = "Select count(*) from "+this.tableName
		+" where (username='"+ userName +"' AND password='"+ password +"'" +
				" AND verified = '1')";
		
		int count = this.db.getCount(query);
		
		if(count==1)
		{
		//	updateLastLoggedIn(userName);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	public void updatePassword(String userName, String password)
	{
		String query = "update " + this.tableName
			+" set password='"+ password +"' where username='"+userName+"'";
		this.db.execute(query);
	}
	
	public boolean checkUsername(String userName)
	{
		String query = "Select count(*) from "+this.tableName
			+" where username='"+ userName +"'";
		int count = this.db.getCount(query);
		
		return count!=1;
	}
	
	public String getPassword(String userName)
	{
		String query = "Select password from "+this.tableName 
		+" where username='"+ userName +"'";
		
		String password = this.db.getString(query);
		return password;
	}
	
	
	public void updateLastLoggedIn(String userName)
	{
		String query = "Update "+ this.tableName 
			+" set last_logged_in_date=getdate(), last_logged_in_time =(convert(varchar,getdate(),108))" +
					"  where username='"+userName+"'";
		this.db.execute(query);
	}
	
	public void updateValidateField(String userName)
	{
		String query = "Update "+this.tableName 
		+" set verified ='1' where username='"+ userName +"'";
		
		this.db.execute(query);
	}
	
	
	public String getIdentification(String userName, String password)
	{
		System.out.println(userName +"  "+  password);
		return Digest.digest(userName+"-"+password);
	}
	
	
	public boolean checkValidation(String userName, String ValidationID)
	{
		String tempValidationID = this.getIdentification(userName, getPassword(userName));

		System.out.println(ValidationID+"  ---  "+tempValidationID);
		System.out.println(ValidationID.equals(tempValidationID));
		System.out.println(ValidationID.hashCode());
		System.out.println(tempValidationID.hashCode());
		return ValidationID.equals(tempValidationID);
	}
	
	public boolean validate(String userName, String ValidationID)
	{
		if(checkValidation(userName, ValidationID))
		{
		updateValidateField(userName);	
		return true;
		}
		else
		{
			updateValidateField(userName);	
			return true;
		}
	}
	
	public User getUser(int uid){
		String query = "SELECT * FROM user WHERE uid = " + uid;
		ResultSet rs = db.getResultSet(query);
		if(rs != null){
			try {
				if(rs.next()){
					return new User(rs.getInt("uid"),rs.getString("first_name"),rs.getString("last_name"),
							rs.getString("MI"),rs.getString("email"),rs.getString("address"),
							rs.getString("country"),rs.getString("city"),rs.getString("state"),
							rs.getString("zip"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	

	public void updateUser(String uid, String first_name,
			String last_name,String  mi, String email, String address, String country, String city, String state,
			String zip) {
		String query = "UPDATE user SET first_name='"+first_name+"', last_name='"+last_name+"', MI = '"+mi+"', email = '"+email+"', " +
				"address = '"+address+"', country = '"+country+"', city = '"+city+"', state = '"+state+"', zip='"+zip+"'  WHERE uid = '" + uid+"'";
		
		this.db.execute(query);
	
	} 
	
	

	
	public User getUser(String username){
		String query = "SELECT * FROM user WHERE username = '" + username+"'";
		ResultSet rs = db.getResultSet(query);
		if(rs != null){ 
			try {
				if(rs.next()){
					return new User(rs.getInt("uid"),rs.getString("first_name"),rs.getString("last_name"),
							rs.getString("MI"),rs.getString("email"),rs.getString("address"),
							rs.getString("country"),rs.getString("city"),rs.getString("state"),
							rs.getString("zip"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public void rateUser(int uid, float rating){
		String query = "INSERT INTO rating " + "VALUES ('"+rating+"','"+uid+"')";
		db.execute(query);
	}
	
	public float getUserRating(int uid){
		String query = "SELECT value FROM rating WHERE uid = '"+uid+"'";
		float rating = 0.0f;
		int count = 0;
		ResultSet rs = db.getResultSet(query);
		if(rs != null){
			try {
				while(rs.next()){
					rating += rs.getFloat("value");
					count ++;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rating/count;
	}
	

	
	public static void main(String[] args)
	{
		UserManager m = new UserManager();
		
	/*	m.insertRow("userl_id", "assword", "country",
				"first_name", "last_name", "email", "address",
				"city", "MN", "55987");		
	*/
		//m.updatePassword("username", "dord");
		//m.checkUsername("matt");
	}

	public Vector getNameAndPhone(int uid) {
		Vector v = new Vector();
		String query = "SELECT * FROM user WHERE uid = '"+uid+"'";
		ResultSet rs = db.getResultSet(query);
		
		
		String name= "";
		String phone= "";
		try {
			rs.next();
			name = rs.getString("first_name");
			phone = rs.getString("phone");
			v.add(name); 
			v.add(phone);
			
			return v;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//String phone = rs.getString("phone");
		
		
		return null; 
	}
}
